package com.project.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.project.bean.EmployeeBean;
import com.project.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

@Controller
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService service;

    @RequestMapping("findByItem")
    public String findByItem(@RequestParam(value = "pageNO", defaultValue = "1") Integer pageNO,
                             Integer startMoney,
                             Integer endMoney,
                             String position,
                             HttpServletRequest req) {
        IPage ip = service.findByItem(pageNO, startMoney, endMoney, position);
        req.setAttribute("ipage", ip);
        req.setAttribute("startMoney", startMoney);
        req.setAttribute("endMoney", endMoney);
        req.setAttribute("position", position);

        return "/index.html";

    }

    @RequestMapping("findById")
    public String findById(Integer id, HttpServletRequest req) {
        EmployeeBean em = service.findById(id);
        req.setAttribute("em", em);
        return "/update.html";
    }

    @RequestMapping("update")
    public String update(Integer id, Integer money) {
        service.update(id, money);
        return "redirect:/employee/findByItem";
    }

    @RequestMapping("del")
    public String del(Integer id) {
        service.del(id);
        return "redirect:/employee/findByItem";
    }

    @RequestMapping("add")
    public String add(String name, String position, Integer money) {
        service.add(new EmployeeBean(name, position, money));
        return "redirect:/employee/findByItem";
    }

}
